home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / inputs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  13.2 KB  |  392 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)inputs.c    V1.21    3/13/95";
  3. #endif
  4. /*
  5. |    file name - inputs.c
  6. |===================================================================
  7. |
  8. |    Example program that shows how to use views that contain
  9. |       input objects.
  10. |
  11. |       The view contains a menu and a slider.  The slider
  12. |    affects a graph that is found in the view.  The variable
  13. |       descriptors of the two objects have been pointed to the
  14. |       same data source variable.  Therefore, when the slider
  15. |       is moved, the data source variable's buffer address is
  16. |       modified to reflect the change which will be reflected
  17. |       in the graph after it has been updated.  The menu is an
  18. |       action menu used to draw and erase a circle and a
  19. |    rectangle object found in the view.  The menu selection
  20. |    determines which action is to be performed.  A service
  21. |    result request is posted on the input object to determine
  22. |    when a selection has been made. The service result code
  23. |       INPUT_DONE is generated for a menu selection. The
  24. |       service result routine HandleInput is called once a
  25. |       selection is made and the buffer address of the data
  26. |       source variable is examined to determine the course of
  27. |       action to be taken.
  28. |
  29. |    Typing a <q> or <Q> will exit the program.
  30. |
  31. |===================================================================
  32. */
  33. #include<windows.h>
  34. /*
  35.  *  DV-Tools header files
  36.  */
  37. #include "std.h"                /* <stdio.h> etc., scalar & macro definitions */
  38. #include "dvstd.h"              /* public types & constants */
  39. #include "dvtools.h"            /* constants used by T routines */
  40. #include "dvGR.h"               /* constants used by window mgt & GR routines */
  41. #include "VOstd.h"              /* constants used by VO & VOob routines */
  42. #include "Tfundecl.h"           /* T routines (screens, drawports & views) */
  43. #include "VOfundecl.h"          /* VO routines (objects) */
  44. #include "VUerfundecl.h"        /* VUer routines (event handling routines) */
  45.  
  46. /* Constants */
  47. #define  DVPATH            (char *)NULL
  48. #define  DISPFORMS_STB     (char *)NULL
  49. #define  DVDEVICE          (char *)NULL
  50. #define  DVCOLORTABLE      (char *)NULL
  51. #define  VIEW_NAME       "inputs.v"
  52. #define  SCREEN_VIEWPORT   (RECTANGLE *)NULL
  53. #define  CLIENT           (OBJECT)1
  54. #define  DONE_LABEL       (int)1
  55. #define  DRAW_CIRCLE       (int)1
  56. #define  ERASE_CIRCLE       (int)2
  57. #define  DRAW_RECT       (int)3
  58. #define  ERASE_RECT       (int)4
  59.  
  60. /* Whole world rectangle, (-16384, -16384) to (16383, 16383)*/
  61. RECTANGLE whole_world = {XMIN, YMIN, XMAX, YMAX};
  62.  
  63. /*
  64.  * Information to be passed on to the service result
  65.  * routine HandleInput().
  66.  */
  67. typedef struct
  68. {
  69.   DRAWPORT drawport;           /* how & where to display picture, picture frame */
  70.   OBJECT circle;               /* circle object in view */
  71.   OBJECT rectangle;            /* rectangle object in view */
  72.   float *data_ptr;             /* buffer address of data source variable */
  73. } INFO;
  74.  
  75. /* Functions defined in inputs.c */
  76. int main V_P_((int argc, char *argv[]));
  77. int HandleInput V_P_((OBJECT client, EVENT_REQUEST er, int label,
  78.               OBJECT loc, ADDRESS args));   /* service result routine */
  79.  
  80. /*
  81.  *   MAIN PROGRAM
  82.  */
  83. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  84.                      LPSTR lpCmdLine,  int nCmdShow  )
  85. {
  86.   INT argc = 0;
  87.   CHAR **argv;
  88.  
  89.   /*
  90.    *  program arguments
  91.    *    argv[1] - display device (default is DVDEVICE)
  92.    */
  93.  
  94.   /* Define & initialize device name and view filename */
  95.   char *device_name = DVDEVICE; /* default device name */
  96.   char *view_name = VIEW_NAME;  /* default view name */
  97.  
  98.   /* Define display variables */
  99.   OBJECT screen;                /* display device, the window */
  100.   VIEW view;                    /* picture representation of the view file */
  101.  
  102.   /* Control loop variables */
  103.   OBJECT location;              /* the event representation */
  104.  
  105.   /* Input object related variables */
  106.   OBJECT drawing,               /* graphical representation of screen */
  107.     menu;                       /* text menu input object */
  108.   ADDRESS *vdplist;             /* variable descriptor list for menu */
  109.   int numvars;                  /* number of vdps in list */
  110.   DSVAR dsvar;                  /* data source variable bound to vdp */
  111.   INFO info;                    /* struct passed to service result routine */
  112.   int event_req_status;         /* status of event requests */
  113.  
  114.   /* Other variables */
  115.   int Quit = NO;                /* flag to quit program */
  116.  
  117.   /*-----------------
  118.    *   Initialization
  119.    *
  120.    *   TInit:    perform the initialization of DV-Tools
  121.    *             TInit reads your configuration file and any
  122.    *             environment variables or logical names set.
  123.    */
  124.   TInit (DVPATH, DISPFORMS_STB);
  125.  
  126.   /*
  127.    *   TscOpenSet:  opens a device as a screen object using
  128.    *                specified attributes
  129.    *
  130.    *   Set exposure block to YES to insure the window
  131.    *   is ready for drawing when TdpDraw is called.
  132.    */
  133.   if (argc > 1)
  134.     device_name = argv[1];
  135.   screen = TscOpenSet (device_name, DVCOLORTABLE,
  136.                        V_X_EXPOSURE_BLOCK, YES,
  137.                        V_ACTIVE_CURSOR, V_END_OF_LIST);
  138.   if (!screen)
  139.     {
  140.       printf ("Must specify device on command line or");
  141.       printf (" in DataViews configuration file.\n");
  142.       S_EXIT (EXIT_ERR);
  143.     }
  144.  
  145.   /*
  146.    *   VOscWinEventMask:  sets the screen's window event mask
  147.    */
  148.   VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS |
  149.                             V_EXPOSE | V_RESIZE | V_MOTIONNOTIFY,
  150.             (ULONG) 0);
  151.  
  152.   /*
  153.    *   TviLoad:   Load a view in from a file,
  154.    *   TdpCreateStretch: Create a drawport with stretched coordinates
  155.    *              The drawport is attached to the screen object
  156.    *              specified while view specifies the view to be
  157.    *              displayed on the screen.
  158.    *          Objects of the view are stretched to make the
  159.    *          the portion of the view specified as whole_world
  160.    *          fit in the specified viewport, SCREEN_VIEWPORT.
  161.    */
  162.   view = TviLoad (view_name);
  163.   if (!view)
  164.     {
  165.       printf ("Could not load view from file ");
  166.       printf ("%s.\n", view_name);
  167.       S_EXIT (EXIT_ERR);
  168.     }
  169.   info.drawport = TdpCreateStretch (screen, view,
  170.                                     SCREEN_VIEWPORT, &whole_world);
  171.  
  172.   /*
  173.    *   TviGetDrawing:     Gets a view's drawing object
  174.    *   TdrGetNamedObject: Gets a named object from a
  175.    *              drawing.
  176.    *
  177.    *   Get the circle, rectangle and text menu input
  178.    *   objects from the view's drawing.
  179.    */
  180.   drawing = TviGetDrawing (view);
  181.   info.circle = TdrGetNamedObject (drawing, "circle.obj");
  182.   info.rectangle = TdrGetNamedObject (drawing, "rect.obj");
  183.   menu = TdrGetNamedObject (drawing, "menu.input");
  184.  
  185.   /*
  186.    *   VOinGetVarList:  Gets a variable descriptor list of
  187.    *            the input object.
  188.    *   TvdGetDataSourceVariable:  Gets the data source variable
  189.    *                  which the variable descriptor
  190.    *                  (vdp) is linked to.
  191.    *   TdsvGetBuffer:   Gets data source variable (dsv)
  192.    *            buffer address.
  193.    *
  194.    *   Obtain the vdp list for the text menu input object.
  195.    *   Find the data source variable which the vdp is linked
  196.    *   to. Then obtain the buffer address which the data
  197.    *   source variable is pointing to.
  198.    */
  199.   VOinGetVarList (menu, &vdplist, &numvars);
  200.   dsvar = TvdGetDataSourceVariable (vdplist[0]);
  201.   info.data_ptr = (float *) TdsvGetBuffer (dsvar);
  202.  
  203.   /*
  204.    *   VUerServiceResultPost:  Post a service result request
  205.    *                   with the event handler.
  206.    *
  207.    *   Post a service result request which monitors the
  208.    *   text menu input object. The request specifies the
  209.    *   type of the service result flag to be generated as
  210.    *   an INPUT_DONE.  INPUT_DONE indicates an input sequence
  211.    *   has been completed (a menu selection was made).
  212.    */
  213.   VUerServiceResultPost (CLIENT, (VUERFCNFUNPTR)HandleInput,
  214.              (ADDRESS) & info, (int)sizeof (info), menu,
  215.              (int)INPUT_DONE, DONE_LABEL);
  216.  
  217.   /*
  218.    *   TscErase: Erase the entire screen in the default
  219.    *             background color
  220.    *   TdpDraw:  Draw the contents of the drawport
  221.    */
  222.   TscErase (screen);
  223.   TdpDraw (info.drawport);
  224.  
  225.   /*--------------------
  226.    *   Control loop
  227.    *
  228.    *   Poll the event queue for window events. If the key
  229.    *   represents the character 'q' or 'Q' then quit the program.
  230.    *   Events occurring within input objects will be handled
  231.    *   through the event request handler VUerHandleLocEvent
  232.    *   and continually update the dynamic objects.
  233.    */
  234.   FOREVER
  235.   {
  236.     /*
  237.      * VOloWinEventPoll:   Poll for the next window event.
  238.      *                     The polling mode used is V_NO_WAIT.
  239.      *                     Using this mode, VOloWinEventPoll
  240.      *                     does not wait until a masked event
  241.      *                     is generated.
  242.      *
  243.      * VUerHandleLocEvent: Service the event. This routine will check
  244.      *                     if the event is used by any input objects
  245.      *                     that may be in the view.
  246.      */
  247.     location = VOloWinEventPoll (V_NO_WAIT);
  248.     if (location)
  249.       {
  250.         event_req_status = VUerHandleLocEvent (location);
  251.  
  252.         /*
  253.          * If the return value of VUerHandleLocEvent is INPUT_UNUSED
  254.          * then check for keypress or buttonpress events which
  255.          * represent one of the exit keys as well as resize
  256.          * and expose events.
  257.          */
  258.         if (event_req_status == INPUT_UNUSED)
  259.           {
  260.             /*
  261.              *  VOloType:  returns the type of event.  These types
  262.              *             match event types specified in VOscWinEventMask.
  263.              */
  264.             switch (VOloType (location))
  265.               {
  266.  
  267.               case V_RESIZE:
  268.                 /*
  269.                  *  The window size has been changed.
  270.                  *  TscReset:  Resets all screen drawports after
  271.                  *             window resizing
  272.                  */
  273.                 TscReset (screen);
  274.                 break;
  275.  
  276.               case V_EXPOSE:
  277.                 /*
  278.                  *  VOloRegion:  Returns a rectangle representing the
  279.                  *               exposed region on the screen.
  280.                  *  TscRedraw:   After erasing, redraws all the drawports
  281.                  *               in the screen.
  282.                  *  A portion of the window has been exposed and needs
  283.                  *  to be redrawn.
  284.                  */
  285.                 TscRedraw (screen, VOloRegion (location));
  286.                 break;
  287.  
  288.               case V_KEYPRESS:
  289.                 /*
  290.                  *  Check key selected.
  291.                  *  VOloKeySym:  Returns the key symbol value of the
  292.                  *               location object
  293.                  *
  294.                  *  If the key symbol represents the characters 'q'
  295.                  *  or 'Q' then quit the program.
  296.                  */
  297.                 switch (VOloKeySym (location))
  298.                   {
  299.                   case 'q':
  300.                   case 'Q':
  301.                     Quit = YES;
  302.                     break;
  303.  
  304.                   default:
  305.                     break;
  306.                   }
  307.                 break;
  308.  
  309.               case V_BUTTONPRESS:
  310.               case V_MOTIONNOTIFY:
  311.               default:
  312.                 break;
  313.               }
  314.           }
  315.       }
  316.  
  317.     /* exit the program */
  318.     if (Quit == YES)
  319.       break;
  320.  
  321.     /*
  322.      *  TdpDrawNext: Update all dynamic objects within a
  323.      *               drawport's view.
  324.      * Update dynamics
  325.      */
  326.     TdpDrawNext (info.drawport);
  327.   }
  328.  
  329.   /*--------------------
  330.    *   Termination
  331.    *
  332.    *   VUerClearAll: Clears a client's event requests
  333.    *   TdpDestroy:   Destroy the drawport,
  334.    *   TviDestroy:   Destroy the view, freeing the allocated memory
  335.    *   TscCloseCurrentScreen:  Close the current display screen
  336.    *   TTerminate:   Perform the clean-up for DV-Tools
  337.    */
  338.   VUerClearAll (CLIENT);
  339.   TdpDestroy (info.drawport);
  340.   TviDestroy (view);
  341.   TscCloseCurrentScreen ();
  342.   TTerminate ();
  343.   return EXIT_OK;
  344. }
  345.  
  346.  
  347. /*--------------
  348.  *   HandleInput -- Perform the appropriate action based
  349.  *      on the user's menu selection.  A circle or
  350.  *    rectangle object is drawn or erased.
  351.  */
  352. /*ARGSUSED*/
  353. int 
  354. HandleInput (client, er, label, loc, args)
  355.      OBJECT client;
  356.      EVENT_REQUEST er;
  357.      int label;
  358.      OBJECT loc;
  359.      ADDRESS args;
  360. {
  361.  
  362.   INFO *info = (INFO *)args;
  363.   /*
  364.    *   TdpDrawObject:   Draw a specific object within a drawport
  365.    *   TdpEraseObject:  Erase an object within a drawport
  366.    *
  367.    *   Examine the data source variable buffer address which
  368.    *   is updated by the input object when a selection has
  369.    *   been made.
  370.    */
  371.   switch ((int) (*info->data_ptr))
  372.     {
  373.     case DRAW_CIRCLE:
  374.       TdpDrawObject (info->drawport, info->circle);
  375.       break;
  376.  
  377.     case ERASE_CIRCLE:
  378.       TdpEraseObject (info->drawport, info->circle);
  379.       break;
  380.  
  381.     case DRAW_RECT:
  382.       TdpDrawObject (info->drawport, info->rectangle);
  383.       break;
  384.  
  385.     case ERASE_RECT:
  386.       TdpEraseObject (info->drawport, info->rectangle);
  387.       break;
  388.     }
  389.  
  390.   return (int) INPUT_USED;
  391. }
  392.